package com.huizhonglm.diap.ui.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.wallet.BankCardInfo;
import com.huizhonglm.diap.core.utils.TPWDUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.presenters.wallet.BankCardListPersenter;
import com.huizhonglm.diap.presenters.wallet.WithdrawPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.wallet.iView.BankCardListIView;
import com.huizhonglm.diap.ui.wallet.iView.WithdrawIView;
import com.huizhonglm.diap.widget.LoadView;

import java.util.List;

/**
 * 提现
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class WithdrawActivity extends BaseActivity implements WithdrawIView, BankCardListIView,
        LoadView.OnLoadAgainListener, AdapterView.OnItemClickListener, View.OnClickListener {

    private LoadView mLoadView;
    private ListView mListView;
    private EditText amountEdt;
    private Button withdrawBtn;

    private List<BankCardInfo> mData;
    private WithdrawAdapter mAdapter;

    private Double balance;
    private Double money;
    private String cardId;

    private WithdrawPresenter wdPresenter;
    private BankCardListPersenter bclPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        setBackEvent(this, null);
        wdPresenter = new WithdrawPresenter(this);
        bclPresenter = new BankCardListPersenter(this);
        balance = getIntent().getDoubleExtra("balance", 0D);
        assignViews();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        View scrlView = findViewById(R.id.withdraw_scrl);
        mLoadView = (LoadView) findViewById(R.id.load_view);
        mListView = (ListView) findViewById(R.id.bank_card_list);
        amountEdt = (EditText) findViewById(R.id.amount_edt);
        withdrawBtn = (Button) findViewById(R.id.withdraw_btn);
        mLoadView.setDataView(scrlView, this);
        mListView.setOnItemClickListener(this);
        withdrawBtn.setOnClickListener(this);
    }

    @Override
    public void onLoadAgain() {
        mLoadView.showProgress();
        bclPresenter.loadBankCards();
    }

    @Override
    public void onLoadCardsSuccess(List<BankCardInfo> data) {
        if (!EmptyUtil.isEmpty(data)) {
            mData = data;
            if (mAdapter == null) {
                mAdapter = new WithdrawAdapter(getLayoutInflater(), mData);
                mListView.setAdapter(mAdapter);
            } else {
                mAdapter.setData(mData);
                mAdapter.notifyDataSetChanged();
            }
            mLoadView.showDataView();
        } else {
            mLoadView.showEmptyView("您还没有添加银行卡呢");
        }
    }

    @Override
    public void onLoadCardsFailure() {
        mLoadView.showErrorView();
    }

    @Override
    public void onWithdrawSuccess() {
        hideProgressBar();
        withdrawBtn.setEnabled(true);
        ToastUtil.makeTextShort("提现成功");
        finish();
    }

    @Override
    public void onWithdrawFailure(String msg) {
        hideProgressBar();
        withdrawBtn.setEnabled(true);
        ToastUtil.makeTextShort("提现失败，请稍后重试");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            onLoadAgain();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.withdraw_btn:
                String amount = amountEdt.getText().toString().trim();
                if (EmptyUtil.isEmpty(amount)) {
                    ToastUtil.makeTextShort("请输入提现金额");
                    return;
                }
                money = Double.valueOf(amount);
                if (money > balance) {
                    ToastUtil.makeTextShort("提现金额已超出余额");
                    return;
                }
                if (money < 200) {
                    ToastUtil.makeTextShort("提现金额必须大于200元");
                    return;
                }
                if ((money % 100) > 0) {
                    ToastUtil.makeTextShort("提现金额必须是100的倍数");
                    return;
                }
                cardId = "";
                SparseBooleanArray sba = mListView.getCheckedItemPositions();
                for (int i = 0; i < sba.size(); i++) {
                    int key = sba.keyAt(i);
                    if (sba.get(key)) {
                        cardId = mData.get(key).getCbaId();
                    }
                }
                if (EmptyUtil.isEmpty(cardId)) {
                    ToastUtil.makeTextShort("请请选择银行卡");
                    return;
                }

                final TPWDUtil tpwdUtil = new TPWDUtil(this, new TPWDUtil.OnInputCompleteListener() {
                    @Override
                    public void onInputCompleted(String tpwd) {
                        showProgressBar();
                        withdrawBtn.setEnabled(false);
                        wdPresenter.onWithdraw(cardId, money, tpwd);
                    }
                });
                tpwdUtil.showDialog();
                break;
        }
    }


}